<?php
/**
* @Copyright Copyright (C) 2010- Gary Teh Name1price.Com
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
**/   
require_once(dirname(__FILE__)."/file_uploader.php"); //Load Biz-Directory Config File

$file = $_FILES['Filedata']['name'];

$new_path = '../../../images/stories/downloads/'; 
$new_file = str_replace(' ', '_', $file);
//$new_file1 = str_replace(' ', '__', $file);  
/*
if(!JFolder::exists($new_path)){
    JFolder::create($new_path);    
}
*/
if(!file_exists($new_path)){
    @mkdir($new_path, '0755');    
} 

//ensures only xml files are uploaded.
if(strtolower(substr($_FILES['Filedata']['name'], (strlen($file))-4, 4))== '.php'){
    $result = array(
        'msg'       => 'Sorry for security purposes uploading of PHP files are allowed.',
        'file'      => '',
        'status'    => 'Security Problem',
        'success'   => '0',
    );
    
    echo json_encode($result);
    exit();
}
else if(is_file($new_path.$new_file)){
    //echo "Sorry only XML Workfow file with Name [".$_FILES['Filedata']['name']."] already exist";
    
    $result = array(
        'msg'       => 'Sorry, file with name: "' . $file . '" already exist',
        'file'      => $file,
        'status'    => 'File Exist',
        'success'   => '0',
    );
    
    echo json_encode($result);
    exit();
}

$file_name = file_uploader_upload_normal($_FILES['Filedata'],  $new_path);  
$result = array(
        'msg'       => 'Upload completed for file "' . $_FILES['Filedata']['name'] . '"',
        'file'      => 'images/stories/downloads/' . $new_file,
        'status'    => 'Completed',
        'success'   => '1',
    );        
echo json_encode($result);
exit();

?>